package com.geek.javase.ch18.videodemo.reflect.singleton;

//枚举-多例
//举例1
/*
public enum MyEnum {
    INSTANCE;
    public static MyEnum getInstance() {
        return INSTANCE;
    }
}
class Test4{
    public static void main(String[] args) {
        MyEnum myEnum1 = MyEnum.getInstance();
        MyEnum myEnum2 = MyEnum.getInstance();
        System.out.println(myEnum1);
        System.out.println(myEnum2);
        System.out.println(myEnum1 == myEnum2);  //true
    }
}
*/
/*
//举例2
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public enum MyEnum {
    INSTANCE;
    public static MyEnum getInstance() {
        return INSTANCE;
    }
}
class Test4{
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
       //通过反射获取枚举
        //1.获取枚举的类类型
        Class<MyEnum> clazz = MyEnum.class;

        //2.查看字节码文件
        //方式1：java -p MyEnum.class准确度不高
        Constructor<MyEnum> declaredConstructor = clazz.getDeclaredConstructor();
        //3.取消访问权限检查，获得构造器，并创建实例
        declaredConstructor.setAccessible(true);
        MyEnum myEnum = declaredConstructor.newInstance();
        System.out.println("myEnum = "+myEnum);
    }
}
*/

//举例3
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public enum MyEnum {
    INSTANCE;
    public static MyEnum getInstance() {
        return INSTANCE;
    }
}
class Test4{
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //通过反射获取枚举
        //1.获取枚举的类类型
        Class<MyEnum> clazz = MyEnum.class;

        //2.查看字节码文件
        //方式1：java -p MyEnum.class准确度不高
        //Constructor<MyEnum> declaredConstructor = clazz.getDeclaredConstructor();

        //方式2：通过IDEA工具，View->Show Bytecode
        Constructor<MyEnum> declaredConstructor = clazz.getDeclaredConstructor(String.class, int.class);
        //3.取消访问权限检查，获得构造器，并创建实例
        declaredConstructor.setAccessible(true);
        MyEnum myEnum = declaredConstructor.newInstance();
        System.out.println("myEnum = "+myEnum);
    }
}


